<?php
/**
 * Created by PhpStorm.
 * User: Sunmer
 * Date: 2019/6/2 0002
 * Time: 1:39
 * Email:Sunmer@foxmail.com
 */
namespace app\api\controller;
use think\Db;
/**
 * 用户信息
 * Class Msgup
 * @package app\api\controller
 */
class Msgup extends BaseController{
    public function user()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        //处理数据
        $result = DB::table('tp_user')->where('id',$res['id'])->find();
        if ($result)
        {
            return $this->ajax_return($result,'获取成功','200');
        }else
        {
            return $this->ajax_return('null','获取失败','201');
        }
    }
    /**
     * 用户名更改
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function name()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id'])||empty($res['name']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        //用户名校验
//        if (Db::table('tp_user')->where('member_name',$res['name'])->find())
//        {
//            return $this->ajax_return('null','用户名已存在','201');
//        }
        //处理数据
        $data = [
            'member_name'=>$res['name'],
            'update_at'=>date('Y-m-d H:i:s',time()),
        ];
        $result = DB::table('tp_user')->where('id',$res['id'])->update($data);
        if ($result)
        {
            return $this->ajax_return($data,'更改昵称成功','200');
        }else
        {
            return $this->ajax_return('null','更改昵称成功','201');
        }
    }

    /**
     * 更改性别
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function sex()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id'])||empty($res['sex']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        //处理数据
        $data = [
            'member_sex'=>$res['sex'],
            'update_at'=>date('Y-m-d H:i:s',time()),
        ];
        $result = DB::table('tp_user')->where('id',$res['id'])->update($data);
        if ($result)
        {
            return $this->ajax_return($data,'更改性别成功','200');
        }else
        {
            return $this->ajax_return('null','更改性别失败','201');
        }
    }

    /**
     * 反馈意见
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function feed()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id'])||empty($res['feed']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        //处理数据
        $data = [
            'uid'=>$res['id'],
            'feedback'=>$res['feed'],
            'create_at'=>date('Y-m-d H:i:s',time()),
        ];
        $result = DB::table('tp_feed')->insert($data);
        if ($result)
        {
            return $this->ajax_return($data,'反馈成功','200');
        }else
        {
            return $this->ajax_return('null','反馈失败','201');
        }
    }

    /**
     * 消息
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function message()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id'])||empty($res['page']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        //处理数据
        $data = [
            'uid'=>$res['id'],
        ];
        $data = $result = DB::table('tp_message')->where($data)->paginate(10,false,[
            'page'=>$res['page']
        ]);
        if ($result)
        {
            return $this->ajax_return($data,'查询成功','200');
        }else
        {
            return $this->ajax_return('null','数据为空','201');
        }
    }

    // 二维码
    public function qrcode()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['id'])||empty($res['page']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //id校验
        if (!Db::table('tp_user')->where('id',$res['id'])->find())
        {
            return $this->ajax_return('null','该用户不存在','201');
        }
        $savePath = APP_PATH . '/../Public/qrcode/';
        $webPath = '/qrcode/';
        $qrData = $this->domain().'/admin/user/qr_code?id=55';
        $qrLevel = 'H';
        $qrSize = '8';
        $savePrefix = 'TKWZ';

        if($filename = $this->createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
            $pic = $webPath . $filename;
        }
        echo "<img src='".$pic."'>";
    }
}